package com.jprocms.module.cms.controller.portal.page;

import com.jprocms.module.cms.dal.dataobject.site.SiteDO;
import com.jprocms.module.cms.service.content.ContentCountService;
import com.jprocms.module.cms.service.site.SiteService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * 前台搜索 Controller 跳转页面
 *
 * @author jprocms
 */
@Controller
public class SearchController {
    private final SiteService siteService;
    private final ContentCountService contentCountService;

    public SearchController(SiteService siteService, ContentCountService contentCountService) {
        this.siteService = siteService;
        this.contentCountService = contentCountService;
    }

    private static final String TEMPLATE = "sys/search";

    @GetMapping({"/search", "/{siteVisitPath:[\\w-]+}/search"})
    public String search(@PathVariable(required = false) String siteVisitPath) {
        SiteDO site = siteService.getSite(siteVisitPath);
        contentCountService.viewIndex(site.getId());
        return site.joinTemplate(TEMPLATE);
    }
}
